//
//  CententViewController.m
//  抽屉效果与UIPageViewController协作
//
//  Created by apple on 15/9/24.
//  Copyright © 2015年 戴维营. All rights reserved.
//

#import "CententViewController.h"
#import "NextViewController.h"



@interface CententViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    CententViewController *cententCtrl;

    NSArray *_viewArray;
    
    UIScrollView *_scrollView;
    
    NSInteger *_currentIndex;
}

@end

@implementation CententViewController

- (instancetype)init
{
    if (self = [super initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil])
    {
        
    }
        
    return self;
}

- (NSArray *)PageViewCtrl
{
    cententCtrl = [[CententViewController alloc] init];
    
    NextViewController *nextCtrl = [NextViewController new];
    
    return @[cententCtrl,nextCtrl];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    _viewArray = [self PageViewCtrl];
    
    [self setViewControllers:@[_viewArray[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    self.dataSource = self;
    
    self.delegate = self;
    
    _scrollView = [self scrollView:self.view];
    
    
    [_scrollView addObserver:self forKeyPath:@"contentOffsrt" options:NSKeyValueObservingOptionNew context:NULL];
}

- (UIScrollView *)scrollView:(UIView *)view
{
    for (UIView *v in view.subviews)
    {
        if ([v isKindOfClass:[UIScrollView class]])
        {
            return (UIScrollView *)v;
        }
        else
        {
            UIView *sv = [self scrollView:v];
            if (sv)
            {
                return (UIScrollView *)sv;
            }
        }
    }
    return nil;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    NSValue *value = change[NSKeyValueChangeNewKey];
    
    CGPoint point = value.CGPointValue;
    
    if (_currentIndex == 0 && point.x < 0)
    {
        _scrollView.panGestureRecognizer.enabled = NO;
        _s
        
    }
}

#pragma mark - UIPageViewControllerSource

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (_currentIndex)
    {
        return nil;
    }
    else
    {
        return _viewArray[1];
    }
}

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (_currentIndex)
    {
        return _viewArray[0];
    }
    else
    {
        return nil;
    }
}

#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    _currentIndex = [_viewArray indexOfObject:pageViewController.viewControllers.firstObject];
}

- (void)dealloc
{
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
@end
